#pragma once

#include "Ribbonview.h"
#include "TabContainer.h"
#include "RichEditTabView.h"
#include "CompilerOutputTabView.h"
#include "RibbonResultManagerView.h"
#include <memory>

#define RibbonCompilersTabId L"RibbonCompilersTabId"
#define	RibbonXMLSourceTabId L"RibbonXMLSourceTabId"
#define	RibbonHeaderFileTabId L"RibbonHeaderFileTabId"
#define	RibbonResourceFileTabId L"RibbonResFileTabId"

class CRibbonXMLSourceTabView : public CRichEditTabView
{
public:
	CRibbonXMLSourceTabView() : CRichEditTabView(L"XML Source", RibbonXMLSourceTabId) {}
	virtual bool IsReadOnly() { return false; }
};

class CRibbonHeaderFileTabView : public CRichEditTabView
{
public:
	CRibbonHeaderFileTabView() : CRichEditTabView(L"Header File", RibbonHeaderFileTabId) {}
};

class CRibbonResourceFileTabView : public CRichEditTabView
{
public:
	CRibbonResourceFileTabView() : CRichEditTabView(L"Resource File", RibbonResourceFileTabId) {}
};

INT_PTR CALLBACK ToolPathsDialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

class CMainView : public CRibbonView
{
	friend INT_PTR CALLBACK ToolPathsDialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	friend INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

private:
	static HWND s_hMainViewWnd; 
	static HINSTANCE s_hInstance;
	
	shared_ptr<CTabContainer> m_tabContainer;
	
	shared_ptr<CCompilerOutputTabView> m_ribbonCompilersTabView;
	shared_ptr<CRibbonXMLSourceTabView> m_ribbonXMLSourceTabView;
	shared_ptr<CRibbonHeaderFileTabView> m_ribbonHeaderFileTabView;
	shared_ptr<CRibbonResourceFileTabView> m_ribbonResourceFileTabView;
	HWND m_hRibbonResultManagerViewWnd;

	wstring m_xmlFilePath;
	CComObject<CRibbonResultManagerView> *m_pRibbonResultManagerView;

	STDMETHOD(OnAfterCreateRibbon)(HWND hWnd, HINSTANCE hInstance);

	RECT GetTabContainerRectangle() const;

	template<class TTabView>
	void ClearOrCreateTabView(shared_ptr<TTabView> &tabView)
	{
		if (tabView)
		{
			tabView->GetRichEdit()->Clear();
		}
		else
		{
			tabView = shared_ptr<TTabView>(new TTabView());
			m_tabContainer->AddTab(tabView);
		}
	}

	void CreateRibbonXMLSourceTab();

	HRESULT ShowRibbon();
	HRESULT ToolPaths();
	HRESULT ShowAboutDialog() const;
	void SetWindowTitle(const wstring &info);
	HRESULT Save();
	HRESULT SaveAs();
	HRESULT Load();
	HRESULT Exit();
	HRESULT Copy();

	static void AddPathCommon7IDEDir();

	static HRESULT LoadSettings();
	static HRESULT SaveSettings();
	void CreateResultManager(const shared_ptr<CRibbonInfo> &ribbonInfo, const wstring &szResourceDllName, PCWSTR szRibbonResourceName);
	void LoadRibbonFromFile(const wstring& szFileName);
	void SaveRibbonToFile(const wstring& szFileName);

public:

	void TabChanged() { m_tabContainer->TabChanged(); }
	void UpdateWindowSise(RECT parentRect);
	bool IsMainWindowValid() const { return IsWindow(s_hMainViewWnd) == TRUE; }

	STDMETHOD(OnCreateUICommand)(UINT32 nCmdID, __in UI_COMMANDTYPE typeID, __deref_out IUICommandHandler** ppCommandHandler);
    
	STDMETHOD(Execute)(UINT nCmdID,
		UI_EXECUTIONVERB verb, 
		__in_opt const PROPERTYKEY* key,
		__in_opt const PROPVARIANT* ppropvarValue,
		__in_opt IUISimplePropertySet* pCommandExecutionProperties);

};
